package com.apac.dao;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.TypedQuery;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.ParameterExpression;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import com.apac.bom.AbstractBOM;
import com.apac.bom.Reimbursement;

public class ReimbursementDAO extends AbstractDAO {

	@Override
	public AbstractBOM findByKey(String id) {
		// TODO Auto-generated method stub
		int idx = 0;
		try{
			idx = Integer.valueOf(id);
		}catch(NumberFormatException e){
			idx = 0;
		}
		return this.findByKey(idx);
	}

	public Reimbursement findByKey(int id) {
		Reimbursement reimbursement = null;

		reimbursement = em.find(Reimbursement.class, id);

		return reimbursement;
	}
	
	public List<Reimbursement> findByEmployee(String id){
		List<Reimbursement> reimbursements = null;
		CriteriaBuilder cb = em.getCriteriaBuilder();
		CriteriaQuery<Reimbursement> cq = cb.createQuery(Reimbursement.class);
		Root<Reimbursement> root = cq.from(Reimbursement.class);
		ParameterExpression<String> param = cb.parameter(String.class, "empid");
		cq.select(root).where(cb.equal(root.get("employeeProject").get("employee").<Integer>get("empIdNo"), param));
		TypedQuery<Reimbursement> tq = em.createQuery(cq);
		tq.setParameter("empid", id);
		
		reimbursements = tq.getResultList();
		
		return reimbursements;
	}
	
	public List<Reimbursement> findByProject(int id){
		List<Reimbursement> reimbursements = null;
		CriteriaBuilder cb = em.getCriteriaBuilder();
		CriteriaQuery<Reimbursement> cq = cb.createQuery(Reimbursement.class);
		Root<Reimbursement> root = cq.from(Reimbursement.class);
		ParameterExpression<Integer> param = cb.parameter(Integer.class, "projid");
		cq.select(root).where(cb.equal(root.get("employeeProject").<Integer>get("empProjCode"), param));
		TypedQuery<Reimbursement> tq = em.createQuery(cq);
		tq.setParameter("projid", id);
		
		reimbursements = tq.getResultList();
		
		return reimbursements;		
	}
	
	public List<Reimbursement> findByProjectAndEmployee(String empid, int projid){
		List<Reimbursement> reimbursements = null;
		List<Predicate> predicates = new ArrayList<Predicate>();
		CriteriaBuilder cb = em.getCriteriaBuilder();
		CriteriaQuery<Reimbursement> cq = cb.createQuery(Reimbursement.class);
		Root<Reimbursement> root = cq.from(Reimbursement.class);
		predicates.add(cb.equal(root.get("employeeProject").get("employee").<Integer>get("empIdNo"), empid));
		predicates.add(cb.equal(root.get("employeeProject").<Integer>get("empProjCode"), projid));
		cq.select(root).where(predicates.toArray(new Predicate[]{}));
		
		TypedQuery<Reimbursement> tq = em.createQuery(cq);
		reimbursements = tq.getResultList();
		
		return reimbursements;
	}

	@Override
	public List<AbstractBOM> findAll() {
		// TODO Auto-generated method stub
		List<AbstractBOM> reimbursements = null;
		CriteriaBuilder cb = em.getCriteriaBuilder();
		CriteriaQuery<Reimbursement> cq = cb.createQuery(Reimbursement.class);
		Root<Reimbursement> root = cq.from(Reimbursement.class);
		cq.select(root);
		
		TypedQuery tq = em.createQuery(cq);
		reimbursements = tq.getResultList();
		
		return reimbursements;
	}

	@Override
	public boolean update(AbstractBOM entity) {
		// TODO Auto-generated method stub
		try{
			Reimbursement reimbursement = this.findByKey(((Reimbursement) entity).getReimbCode());
			tx.begin();
//			reimbursement.setEmployeeProject(((Reimbursement) entity).getEmployeeProject());
//			reimbursement.setReimbAmt(((Reimbursement) entity).getReimbAmt());
//			reimbursement.setReimbApprvName(((Reimbursement) entity).getReimbApprvName());
//			reimbursement.setReimbDesc(((Reimbursement) entity).getReimbDesc());
//			reimbursement.setReimbMaxLimit(((Reimbursement) entity).getReimbMaxLimit());
//			reimbursement.setReimbPriod(((Reimbursement) entity).getReimbPriod());
			em.merge((Reimbursement)entity);
			em.flush();
			em.clear();
			tx.commit();
			return true;
		}catch(Exception e){
			tx.rollback();
		}finally{
			em.close();
		}
		return false;
	}

	@Override
	public boolean delete(AbstractBOM entity) {
		// TODO Auto-generated method stub
		try{
			Reimbursement reimbursement = this.findByKey(((Reimbursement) entity).getReimbCode());
			tx.begin();
			em.remove(reimbursement);
			em.flush();
			em.clear();
			tx.commit();
			return true;
		}catch(Exception e){
			tx.rollback();
		}finally{
			em.close();
		}
		return false;
	}

}
